package com.itheima.myset;

public class Student implements Comparable<Student> {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public int compareTo(Student o) {
        // 主要判断条件: 年龄升序
        /*
            this: 表示要添加的元素
            o: 集合中已有的元素
         */
        int result = this.age - o.age;
        // 次要条件: 年龄相同时,姓名升序
        result = result == 0 ? this.name.compareTo(o.name) : result;
        //姓名降序
        //result = result == 0 ? o.name.compareTo(this.name) : result;
        return result;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
